#!/bin/bash
# Author: Nathaniel “Gnate” Sigrist <sigrist.nathaniel@gmail.com>
# References:
#   

SRC_DEB="$(pwd)"
TAR=lotus_notes853_linuxUb_en.tar
TMP_INSTALL=/tmp/notes
NOTES_DEB_MODIFY="notes sametime"
NOTES_VERSION="8.5.3.i586"

if [ ! -e "$SRC_DEB/" ]; then
    echo "Could not find $TAR tar file in current folder."
    exit 0
fi

[ -e "$TMP_INSTALL" ] && rm -rf "$TMP_INSTALL"
mkdir -p "$TMP_INSTALL"

cp "$SRC_DEB/$TAR" "$TMP_INSTALL"
cd "$TMP_INSTALL"

# Will need with for compatibility with i386
sudo apt-get install ia32-libs

wget https://launchpad.net/~jcollins/+archive/jaminppa/+build/1482994/+files/getlibs_2.06-0ubuntu1%7Eppa2_all.deb
sudo dpkg -i getlibs_2.06-0ubuntu1~ppa2_all.deb

# Know I need:
sudo mkdir /usr/lib32
sudo getlibs -p libgnomevfs2-0 libgnome2-0 libgnome2-common libgnomeui-0 \
    libavahi-glib1 libbonobo2-0 libpopt0 libbonoboui2-0 liborbit2 gnome-keyring \
    libgnomeprint2.2-0 libgnomeprintui2.2-0 libart-2.0-2 libgnomecanvas2-0

tar xf "$TAR"

for DEB in $NOTES_DEB_MODIFY; do
    P="ibm-lotus-$DEB-$NOTES_VERSION"
    mkdir "$P"
    cd "$P"
    ar x "../$P.deb"

    mkdir DEBIAN
    tar xf data.tar.gz
    tar xf control.tar.gz -C DEBIAN

    cd DEBIAN
    cat control | sed -re "s/^(Pre\-Depends|Depends|Recommends|Conflicts):\s*.*$/\1:/" > control-mod
    mv control-mod control

    cd ..
    rm control.tar.gz
    rm data.tar.gz
    rm debian-binary

    cd ..
    sudo dpkg-deb -b "$P"
    rm -rf "$P"

    sudo dpkg -i --force-all $P.deb
done

echo "Downloading libgdk patches..."
wget http://www.freetechie.com/upload/lotus_notes/libgdk-x11-2.0.so.0
wget http://www.freetechie.com/upload/lotus_notes/libgdk_pixbuf-2.0.so.0
wget http://www.freetechie.com/upload/lotus_notes/libgdk_pixbuf_xlib-2.0.so.0
wget http://www.freetechie.com/upload/lotus_notes/libgtk-x11-2.0.so.0
sudo mv *.so.0 /opt/ibm/lotus/notes

# Make opening attachments work
sudo mv /opt/ibm/lotus/notes/openwith /opt/ibm/lotus/notes/openwith.bak
sudo ln -s $(which gnome-open) /opt/ibm/lotus/notes/openwith

# These were from getlibs (/usr/lib32)
echo "Creating symbolic links for libraries..."
sudo ln -s /usr/lib32/libgnomevfs-2.so.0.2400.4 \
    /opt/ibm/lotus/notes/libgnomevfs-2.so.0
sudo ln -s /usr/lib32/libgnome-2.so.0.3200.1 \
    /opt/ibm/lotus/notes/libgnome-2.so.0
sudo ln -s /usr/lib32/i386-linux-gnu/libgnomeui-2.so.0.2400.5 \
    /opt/ibm/lotus/notes/libgnomeui-2.so.0
sudo ln -s /usr/lib32/i386-linux-gnu/libavahi-glib.so.1.0.2 \
    /opt/ibm/lotus/notes/libavahi-glib.so.1
sudo ln -s /usr/lib32/libbonobo-2.so.0.0.0 \
    /opt/ibm/lotus/notes/libbonobo-2.so.0
sudo ln -s /lib32/i386-linux-gnu/libpopt.so.0 \
    /opt/ibm/lotus/notes/libpopt.so.0
sudo ln -s /usr/lib32/libbonobo-activation.so.4.0.0 \
    /opt/ibm/lotus/notes/libbonobo-activation.so.4
sudo ln -s /usr/lib32/libbonoboui-2.so.0.0.0 \
    /opt/ibm/lotus/notes/libbonoboui-2.so.0
sudo ln -s /usr/lib32/i386-linux-gnu/libORBitCosNaming-2.so.0.1.0 \
    /opt/ibm/lotus/notes/libORBitCosNaming-2.so.0
sudo ln -s /usr/lib32/i386-linux-gnu/libORBit-2.so.0.1.0 \
    /opt/ibm/lotus/notes/libORBit-2.so.0
sudo ln -s /usr/lib32/libgnomeprint-2-2.so.0 \
    /opt/ibm/lotus/notes/libgnomeprint-2-2.so.0
sudo ln -s /usr/lib32/libgnomeprintui-2-2.so.0 \
    /opt/ibm/lotus/notes/libgnomeprintui-2-2.so.0
sudo ln -s /usr/lib32/libart_lgpl_2.so.2 \
    /opt/ibm/lotus/notes/libart_lgpl_2.so.2
sudo ln -s /usr/lib32/libgnomecanvas-2.so.0 \
    /opt/ibm/lotus/notes/libgnomecanvas-2.so.0

#  Bit of a shortcut with pkcs11
sudo ln -s /usr/lib32/i386-linux-gnu/pkcs11 \
    /usr/lib/i386-linux-gnu/

# Hope these were installed:
sudo ln -s /usr/lib/i386-linux-gnu/gtk-2.0/modules/libcanberra-gtk-module.so \
    /opt/ibm/lotus/notes/libcanberra-gtk-module.so

echo "Installing the gtk+2 wrapper..."
sudo apt-get install git libgtk2.0-dev g++-multilib
git clone https://github.com/sgh/lotus-notes_gtk2.23.3.git
cd lotus-notes_gtk2.23.3
make
sudo cp libnotesgtkfix.so notes-wrapper /opt/ibm/lotus/notes

cat /usr/share/applications/notesplugin.desktop | \
    sed -re "s/^(Exec=.+\/notes\s+?)'$/\\1-wrapper'/" > new-LotusNotes8.5.desktop
sudo mv new-LotusNotes8.5.desktop /usr/share/applications/LotusNotes8.5.desktop
cd ..

cd "$SRC_DEB"
